package jobflow.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.application.Application;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import jobflow.entity.Adres;
import jobflow.entity.Calisan;

import org.ajax4jsf.component.html.HtmlActionParameter;
import org.ajax4jsf.component.html.HtmlAjaxCommandLink;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.component.html.HtmlPanel;
import org.richfaces.component.html.HtmlSpacer;
import org.richfaces.component.html.HtmlTab;
import org.richfaces.component.html.HtmlTabPanel;
import com.sun.facelets.FaceletContext;

@SuppressWarnings("deprecation")
@Name("calisanDao")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class CalisanDao implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Calisan calisan = new Calisan();

	private Adres adresBir = new Adres();

	private Adres adresIki = new Adres();

	private Adres adresUc = new Adres();

	private List<Adres> adresList = new ArrayList<Adres>();

	private String path = "/calisan/adresBir.xhtml";

	@In
	EntityManager entityManager;

	@In
	FacesMessages facesMessages;

	@In(required = false, create = true)
	FaceletContext faceletContext;

	private List<HtmlTab> htmlTabList = new ArrayList<HtmlTab>();

	
	private Map<String, Boolean> tabsMap = new HashMap<String, Boolean>();

	  
	
	@RequestParameter
	private Boolean value=true;
		

	
	
	
	@Create
	@Begin(join = true)
	public void begin() {
   
	}

	public void calisanEkle() {
         
		adresBir.setCalisan(calisan);
		adresIki.setCalisan(calisan);
		adresUc.setCalisan(calisan);
		
		
		adresList.add(adresBir);
		adresList.add(adresIki);
		adresList.add(adresUc);

		calisan.setAdresList(adresList);

		entityManager.persist(calisan);

	}

	public void removeDelete(ActionEvent action) {
	  String id = action.getComponent().getId();
	  tabsMap.put(id, value);
	  FacesContext contex = FacesContext.getCurrentInstance();
	  HtmlTab tabPanel = (HtmlTab) contex.getViewRoot().findComponent("calisanForm:"+id+"Id");
      tabPanel.setRendered(false);
	}
	
    Integer k=0;
	public void addTabPanel() throws Exception {

	    k++;
	    
	
		FacesContext contex = FacesContext.getCurrentInstance();
		Application application = contex.getApplication();
		HtmlTab tab = (HtmlTab) application.createComponent(HtmlTab.COMPONENT_TYPE);	
		ResourceBundle bundle = ResourceBundle.getBundle("messages_tr");
		
	

		// Tab Panel
		HtmlTabPanel tabPanel = (HtmlTabPanel) contex.getViewRoot().findComponent("calisanForm:tabpanelId");
		tabPanel.setSwitchType("client");
    
		// Tab
				
		tab.setLabelWidth("150");
		tab.setName(k);
		String componentId = "tab"+Integer.toString(k);
		String tabId = componentId+"Id";//tab1Id
		tab.setId(tabId);
		if(tabsMap.get(componentId)!=null)
		tab.setRendered(tabsMap.get(componentId));
		//<rich:tab label="Tab 1" rendered="#{tabsBean.tabsMap['tab1']}">
		
		tabPanel.getChildren().add(tab);// Tab add to Tab panel
     
		tabsMap.put(tab.getId(),true);
		
	
		// tpanelGroup
		HtmlPanelGroup panelGroup = new HtmlPanelGroup();
		tab.getFacets().put("label", panelGroup);

		// f:facet
		HtmlAjaxCommandLink htmlcommandLink = new HtmlAjaxCommandLink();
		panelGroup.getChildren().add(htmlcommandLink);
		HtmlGraphicImage graphicImage = new HtmlGraphicImage();
		graphicImage.setValue("/images/modal/close.png");
		htmlcommandLink.getChildren().add(graphicImage);
		HtmlOutputText outputText = new HtmlOutputText();
		outputText.setEscape(false);
		outputText.setValue("<b>Adres Bilgisi<b/>");
		panelGroup.getChildren().add(outputText);
		htmlcommandLink.setId(componentId);
		htmlcommandLink.setStyle("position:absolute;left:10px;");
		htmlcommandLink.setAjaxSingle(true);
		htmlcommandLink.setReRender(tabId);
		MethodBinding methodBinding = application.createMethodBinding("#{calisanDao.removeDelete}", new Class[] { ActionEvent.class });
		htmlcommandLink.setActionListener(methodBinding);

		HtmlActionParameter actionParameter = new HtmlActionParameter();
		actionParameter.setName("value");
		actionParameter.setValue("#{!calisanDao.tabsMap[componentId]}");	
		htmlcommandLink.getChildren().add(actionParameter);
		htmlcommandLink.setReRender("tabpanelId");

		// panel
		HtmlPanel htmlPanel = (HtmlPanel) application
		.createComponent(HtmlPanel.COMPONENT_TYPE);		
		htmlPanel.setHeader("Adres Bilgileri");
		tab.getChildren().add(htmlPanel);

		// panelGrid
		HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application
		.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
		htmlPanelGrid.setColumns(3);
		htmlPanel.getChildren().add(htmlPanelGrid);

		// apartman
		HtmlOutputText apartmanAdiOutPutText = (HtmlOutputText) application
		.createComponent(HtmlOutputText.COMPONENT_TYPE);
		apartmanAdiOutPutText.setEscape(false);
		String apatmanAdi = bundle.getString("adres.apartman.adi");
		apartmanAdiOutPutText.setValue("<b>" + apatmanAdi + "<b/>");
		htmlPanelGrid.getChildren().add(apartmanAdiOutPutText);
		HtmlSpacer apartmanAdiSpace = (HtmlSpacer) application
				.createComponent(HtmlSpacer.COMPONENT_TYPE);
		apartmanAdiSpace.setWidth("80");
		apartmanAdiSpace.setHeight("30");
		htmlPanelGrid.getChildren().add(apartmanAdiSpace);
		HtmlInputText apartmanAdiInput = (HtmlInputText) application
				.createComponent(HtmlInputText.COMPONENT_TYPE);
		htmlPanelGrid.getChildren().add(apartmanAdiInput);

		// cadde
		HtmlOutputText caddeOutPutText = (HtmlOutputText) application
		.createComponent(HtmlOutputText.COMPONENT_TYPE);
		caddeOutPutText.setEscape(false);
		String caddeAdi = bundle.getString("adres.cadde");
		caddeOutPutText.setValue("<b>" + caddeAdi + "<b/>");
		htmlPanelGrid.getChildren().add(caddeOutPutText);
		HtmlSpacer caddeSpace = (HtmlSpacer) application
				.createComponent(HtmlSpacer.COMPONENT_TYPE);
		caddeSpace.setWidth("80");
		caddeSpace.setHeight("30");
		htmlPanelGrid.getChildren().add(caddeSpace);
		HtmlInputText caddeInput = (HtmlInputText) application
				.createComponent(HtmlInputText.COMPONENT_TYPE);
		htmlPanelGrid.getChildren().add(caddeInput);

		// sokak
		HtmlOutputText sokakOutPutText = (HtmlOutputText) application
				.createComponent(HtmlOutputText.COMPONENT_TYPE);
		sokakOutPutText.setEscape(false);
		String sokakAdi = bundle.getString("adres.sokak");
		sokakOutPutText.setValue("<b>" + sokakAdi + "<b/>");
		htmlPanelGrid.getChildren().add(sokakOutPutText);
		HtmlSpacer sokakSpace = (HtmlSpacer) application
				.createComponent(HtmlSpacer.COMPONENT_TYPE);
		sokakSpace.setWidth("80");
		sokakSpace.setHeight("30");
		htmlPanelGrid.getChildren().add(sokakSpace);
		HtmlInputText sokakInput = (HtmlInputText) application
				.createComponent(HtmlInputText.COMPONENT_TYPE);
		htmlPanelGrid.getChildren().add(sokakInput);

		// kapino
		HtmlOutputText kapiNoOutPutText = (HtmlOutputText) application
				.createComponent(HtmlOutputText.COMPONENT_TYPE);
		kapiNoOutPutText.setEscape(false);
		String kapiNo = bundle.getString("adres.kapi.no");
		kapiNoOutPutText.setValue("<b>" + kapiNo + "<b/>");
		htmlPanelGrid.getChildren().add(kapiNoOutPutText);
		HtmlSpacer kapiNoSpace = (HtmlSpacer) application
				.createComponent(HtmlSpacer.COMPONENT_TYPE);
		kapiNoSpace.setWidth("80");
		kapiNoSpace.setHeight("30");
		htmlPanelGrid.getChildren().add(kapiNoSpace);
		HtmlInputText kapiNoInput = (HtmlInputText) application
				.createComponent(HtmlInputText.COMPONENT_TYPE);
		htmlPanelGrid.getChildren().add(kapiNoInput);

		// daireNo
		HtmlOutputText daireNoOutPutText = (HtmlOutputText) application
				.createComponent(HtmlOutputText.COMPONENT_TYPE);
		daireNoOutPutText.setEscape(false);
		String daireNo = bundle.getString("adres.daire.no");
		daireNoOutPutText.setValue("<b>" + daireNo + "<b/>");
		htmlPanelGrid.getChildren().add(daireNoOutPutText);
		HtmlSpacer daireNoSpace = (HtmlSpacer) application
				.createComponent(HtmlSpacer.COMPONENT_TYPE);
		daireNoSpace.setWidth("80");
		daireNoSpace.setHeight("30");
		htmlPanelGrid.getChildren().add(daireNoSpace);
		HtmlInputText daireNoInput = (HtmlInputText) application
				.createComponent(HtmlInputText.COMPONENT_TYPE);
		htmlPanelGrid.getChildren().add(daireNoInput);

	}



	public void setCalisan(Calisan calisan) {
		this.calisan = calisan;
	}

	public Calisan getCalisan() {
		return calisan;
	}

	public void setAdresIki(Adres adresIki) {
		this.adresIki = adresIki;
	}

	public Adres getAdresIki() {
		return adresIki;
	}

	public void setAdresUc(Adres adresUc) {
		this.adresUc = adresUc;
	}

	public Adres getAdresUc() {
		return adresUc;
	}

	public void setAdresBir(Adres adresBir) {
		this.adresBir = adresBir;
	}

	public Adres getAdresBir() {
		return adresBir;
	}

	public void setHtmlTabList(List<HtmlTab> htmlTabList) {
		this.htmlTabList = htmlTabList;
	}

	public List<HtmlTab> getHtmlTabList() {
		return htmlTabList;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPath() {
		return path;
	}

	public void setTabsMap(Map<String, Boolean> tabsMap) {
		this.tabsMap = tabsMap;
	}

	public Map<String, Boolean> getTabsMap() {
		return tabsMap;
	}

	


}
